<template>
  <div class="user-container">
    <div class="user-header">
      <h2>用户管理</h2>
      <el-button type="primary" @click="handleAddUser">
        <el-icon><Plus /></el-icon>添加用户
      </el-button>
    </div>

    <div class="search-container">
      <el-form :inline="true" :model="searchForm" class="form-inline">
        <el-form-item label="用户名">
          <el-input v-model="searchForm.username" placeholder="请输入用户名" clearable />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="searchForm.name" placeholder="请输入姓名" clearable />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="searchForm.role" placeholder="请选择角色" clearable>
            <el-option label="系统管理员" value="admin" />
            <el-option label="仓库管理员" value="warehouse_admin" />
            <el-option label="仓库操作员" value="warehouse_operator" />
            <el-option label="数据查看员" value="viewer" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="启用" value="active" />
            <el-option label="禁用" value="inactive" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table-container">
      <el-table :data="userData" border style="width: 100%">
        <el-table-column prop="id" label="用户ID" width="80" />
        <el-table-column prop="username" label="用户名" width="120" />
        <el-table-column prop="name" label="姓名" width="120" />
        <el-table-column prop="phone" label="手机号" width="120" />
        <el-table-column prop="email" label="邮箱" width="180" />
        <el-table-column prop="role" label="角色" width="120">
          <template #default="scope">
            <el-tag v-if="scope.row.role === 'admin'" type="danger">系统管理员</el-tag>
            <el-tag v-else-if="scope.row.role === 'warehouse_admin'" type="warning">仓库管理员</el-tag>
            <el-tag v-else-if="scope.row.role === 'warehouse_operator'" type="success">仓库操作员</el-tag>
            <el-tag v-else-if="scope.row.role === 'viewer'" type="info">数据查看员</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'active' ? 'success' : 'info'">
              {{ scope.row.status === 'active' ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column prop="lastLoginTime" label="最后登录时间" width="180" />
        <el-table-column fixed="right" label="操作" width="200">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">
              删除
            </el-button>
            <el-button
              size="small"
              :type="scope.row.status === 'active' ? 'warning' : 'success'"
              @click="handleToggleStatus(scope.row)"
            >
              {{ scope.row.status === 'active' ? '禁用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <common-pagination
        :total="pagination.total"
        :page-size="pagination.pageSize"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import CommonPagination from '@/components/CommonPagination.vue'

// 搜索表单
const searchForm = reactive({
  username: '',
  name: '',
  role: '',
  status: ''
})

// 分页数据
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 100
})

// 模拟用户数据
const userData = ref([
  {
    id: 1,
    username: 'admin',
    name: '系统管理员',
    phone: '13800138000',
    email: 'admin@example.com',
    role: 'admin',
    status: 'active',
    createTime: '2023-01-01 08:00:00',
    lastLoginTime: '2023-08-15 09:30:00'
  },
  {
    id: 2,
    username: 'manager1',
    name: '张经理',
    phone: '13900139001',
    email: 'manager1@example.com',
    role: 'warehouse_admin',
    status: 'active',
    createTime: '2023-01-10 10:20:00',
    lastLoginTime: '2023-08-14 16:45:00'
  },
  {
    id: 3,
    username: 'operator1',
    name: '李操作员',
    phone: '13700137002',
    email: 'operator1@example.com',
    role: 'warehouse_operator',
    status: 'active',
    createTime: '2023-02-15 09:15:00',
    lastLoginTime: '2023-08-15 11:20:00'
  },
  {
    id: 4,
    username: 'viewer1',
    name: '王查看员',
    phone: '13600136003',
    email: 'viewer1@example.com',
    role: 'viewer',
    status: 'inactive',
    createTime: '2023-03-05 14:30:00',
    lastLoginTime: '2023-07-20 10:10:00'
  },
  {
    id: 5,
    username: 'operator2',
    name: '赵操作员',
    phone: '13500135004',
    email: 'operator2@example.com',
    role: 'warehouse_operator',
    status: 'active',
    createTime: '2023-04-12 11:45:00',
    lastLoginTime: '2023-08-15 08:30:00'
  }
])

// 查询用户
const handleSearch = () => {
  // 实际应用中这里会调用API进行查询
  ElMessage.success('查询条件已应用')
  pagination.currentPage = 1
}

// 重置搜索
const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}

// 分页大小变化
const handleSizeChange = (size) => {
  pagination.pageSize = size
  // 实际应用中这里会调用API重新获取数据
}

// 当前页变化
const handleCurrentChange = (page) => {
  pagination.currentPage = page
  // 实际应用中这里会调用API重新获取数据
}

// 添加用户
const handleAddUser = () => {
  ElMessage.info('打开添加用户表单')
  // 实际应用中这里会打开添加用户对话框
}

// 编辑用户
const handleEdit = (row) => {
  ElMessage.info(`编辑用户: ${row.name}`)
  // 实际应用中这里会打开编辑用户对话框
}

// 删除用户
const handleDelete = (row) => {
  ElMessageBox.confirm(
    `确定要删除用户"${row.name}"吗？`,
    '删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
    .then(() => {
      // 实际应用中这里会调用删除API
      ElMessage.success(`用户"${row.name}"已成功删除`)
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}

// 切换用户状态
const handleToggleStatus = (row) => {
  const newStatus = row.status === 'active' ? 'inactive' : 'active'
  const statusText = newStatus === 'active' ? '启用' : '禁用'
  
  ElMessageBox.confirm(
    `确定要${statusText}用户"${row.name}"吗？`,
    `${statusText}确认`,
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
    .then(() => {
      // 实际应用中这里会调用状态变更API
      row.status = newStatus
      ElMessage.success(`用户"${row.name}"已${statusText}`)
    })
    .catch(() => {
      ElMessage.info(`已取消${statusText}操作`)
    })
}
</script>

<style scoped>
.user-container {
  padding: 20px;
}

.user-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-container {
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.table-container {
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style> 